import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/pages/Home/workbench_page.dart';
import 'package:xiongan_app_yy/pages/common/empty_page.dart';
import 'package:xiongan_app_yy/provider/message_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/badge.dart';
import 'package:xiongan_app_yy/widgets/comm/btm_navigation.dart';

import 'appointment_page.dart';
import 'me_page.dart';
import 'message_page.dart';

//首页
class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  int _current = 0;
  String TYPE_KEY = "isAdmin";
  List<Widget> pageList;
  List<String> titleList;
  List<IconData> iconsList;

  //上次点击时间
  DateTime lastPopTime;

  void setPageIndex(int index) {
    setState(() {
      _current = index;
    });
  }


  final pageController = PageController();


  void onTap(int index) {
    pageController.animateToPage(index, duration: new Duration(milliseconds: 500),curve:Curves.ease);
  }


  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      Provider.of<MessageProvider>(context).getList();
    });
    pageList = [
      //TabbedPage(),
      MessagePage(),
      //FunsPage(),
      MePage()
    ];
    if (DataSingleton().Data.appModuleMenusList.length == 1) {
      pageList.insert(0, AppointmentPage());
    } else if (DataSingleton().Data.appModuleMenusList.length == 3) {
      pageList.insert(0, WorkbenchPage());
    } else {
      pageList.insert(0, EmptyPage());
    }

    titleList = [
      //标题集合
      "首页",
      "消息",
      "我的"
    ];
    iconsList = [
      //图标集合
      Icons.home,
      Icons.sms,
      Icons.person
    ];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    List<BottomNavigationBarItem> items = [];
    for (var i = 0; i < titleList.length; i++) {
      print(Provider.of<MessageProvider>(context).num);
      items.add(BottomNavigationBarItem(
          icon: i == 1 && Provider.of<MessageProvider>(context).num > 0
              ? Badge(
                  offsetX: -10,
                  offsetY: -4,
                  padding: 4,
                  child: Icon(iconsList[i]),
                  text: "${Provider.of<MessageProvider>(context).num}",
                )
              : Icon(iconsList[i]),
          title: Text(titleList[i])));
    }
    return WillPopScope(
        child: Scaffold(
            body: PageView(
              controller: pageController,
              onPageChanged: setPageIndex,
              children: pageList,
              physics:DataSingleton().Data.appModuleMenusList.length == 1? NeverScrollableScrollPhysics():null,
            ),
            bottomNavigationBar: BtmNavigation(
                current: _current, onTabFuns: onTap, itemList: items)),
        onWillPop: () async {
          // 点击返回键的操作
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
            lastPopTime = DateTime.now();
            showToast("再按一次退出", position: ToastGravity.BOTTOM);
            return false;
          } else {
            lastPopTime = DateTime.now();
            // 退出app
            SystemNavigator.pop();
            return true;
          }
        });
  }

  void showToast(String s, {position}) {
    Fluttertoast.showToast(
        msg: "$s",
        toastLength: Toast.LENGTH_SHORT,
        gravity: position,
        timeInSecForIos: 1,
        backgroundColor: Colors.blue,
        textColor: Colors.yellow);
  }
}
